package com.imooc.poker_game;

/**
 * 一张牌
 */
public class PokerPiece implements Comparable<PokerPiece>{
    /**
     * 花色： 黑 红 梅 方
     */
    Integer pattern;
    Integer figure;

    /**
     * 牌面和花色
     * @param pattern Integer
     * @param figure Integer
     */
    public PokerPiece(Integer pattern, Integer figure) {
        this.pattern = pattern;
        this.figure = figure;
    }

    /**
     * 对外展示的内容
     * @return String
     */
    public String show() {
        String figure;
        String pattern;
        switch (this.figure) {
            case 11:
                figure = "J";
                break;
            case 12:
                figure = "Q";
                break;
            case 13:
                figure = "K";
                break;
            case 14:
                figure = "A";
                break;
            default:
                figure = this.figure + "";
                break;
        }

        switch (this.pattern) {
            case 1:
                pattern = "黑桃";
                break;
            case 2:
                pattern = "红桃";
                break;
            case 3:
                pattern = "梅花";
                break;
            case 4:
                pattern = "方块";
                break;
            default:
                pattern = "";
                break;
        }

        return pattern + figure;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (!(obj instanceof PokerPiece)) return false;
        PokerPiece piece = (PokerPiece) obj; //以上都通过，则强转obj进一步判断。
        if (this.pattern == null || this.figure == null) {
            if(piece.pattern == null || this.figure == null) {
                return true;
            } else {
                return false;
            }
        }else{
            if(this.figure.equals(piece.figure)&&this.pattern.equals(piece.pattern))
                return true;
            else
                return false;
        }
    }

    @Override
    public int compareTo(PokerPiece o) {
        // 先比大小，再比花色
        int rule1 = o.figure.compareTo(this.figure);
        if(rule1 == 0){
            return this.pattern.compareTo(o.pattern);
        }else{
            return rule1;
        }
    }
}
